package com.baizhi.Controller;

import com.baizhi.annotation.AddCacheAnnotation;
import com.baizhi.annotation.DeleteCacheAnnotation;
import com.baizhi.annotation.LogAnnotation;
import com.baizhi.service.UserService;
import com.baizhi.service.UserThreadService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserThreadService userThreadService;
    private Logger logger= LoggerFactory.getLogger("UserController.class");
    @LogAnnotation(value="展示所有用户",type = "select")
    @AddCacheAnnotation
    @RequestMapping("showAll")
    public @ResponseBody Map showAll(String userName,int page,int limit){
        Map map=userService.showUser(userName,page,limit);
        return map;
    }
    @LogAnnotation(value = "删除用户",type = "delete")
    @DeleteCacheAnnotation
    @RequestMapping("delete")
    public @ResponseBody Map deleteUserById(int userId){
        Map map=userService.deleteUerById(userId);
        return map;
    }
    @RequestMapping("selectSex")
    public @ResponseBody List<Map> selectSex(){
        List<Map> list= userService.selectCountBySex();
        System.out.println(list+"-============================");
        return list;
    }
    @RequestMapping("selectProvince")
    public @ResponseBody List<Map>selectProvince(){
        List<Map> list=userService.selectCountByProvince();
        return list;
    }
    @RequestMapping("selectAll")
    public @ResponseBody Map selectAll(){
        Map map=userService.showAll();
        return map;
    }
    @RequestMapping("selectAllCountThread")
    @LogAnnotation(value = "优化过得",type = "select")
    @AddCacheAnnotation
    public @ResponseBody Map selectAllCountThread() throws InterruptedException {
        long start =System.currentTimeMillis();
        Map map = userThreadService.selectAllCountThread();
       long end=System.currentTimeMillis();
       logger.debug(end-start+"ms");
        return map;
    }
}
